﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
namespace asMeishi.web.Models
{
    using Classes;
    public class keyWord : MongoDocument
    {
        /// <summary>
        /// 值
        /// </summary>
        public string value { get; set; }
        /// <summary>
        /// 引用次数
        /// </summary>
        public int num { get; set; }
        /*
        /// <summary>
        /// 创建用户
        /// </summary>
        public string userId { get; set; }
        */
        /// <summary>
        /// 创建时间
        /// </summary>
        [BsonDateTimeOptions(Kind = DateTimeKind.Local)]
        public DateTime datetime { get; set; }
        /*lys
        /// <summary>
        /// 重载++运算符
        /// </summary>
        /// <param name="a"></param>
        /// <returns></returns>
        public static keyWord operator ++(keyWord a)
        {
            a.num++;
            return a;
        }*/

    }

    public class keyWordC
    {
        DataSession<MongoDataContext, keyWord> db = new DataSession<MongoDataContext, keyWord>();
            
        /// <summary>
        /// 新建一个keyWord实例 已赋值 datetima userId 并且 num = 1
        /// </summary>
        /// <returns></returns>
        public keyWord createDefault(string val = null)
        {
            var keyWord = new keyWord();
            keyWord.value = val;
            keyWord.datetime = DateTime.Now;
            keyWord.num = 1;
            return keyWord;
        }


        /// <summary>
        /// 将字符存入关键字表
        /// </summary>
        /// <param name="val"></param>
        /// <returns></returns>
        public ObjectId add(string val)
        {
            val = convertVal(val);
            keyWord data;
            if (isExist(val))
            {
                data = db.Get(d => d.value == val);
                data.num++;
                db.Save(data);
                return data.Id;
            }
            data = createDefault(val);
            db.Save(data);
            return data.Id;
        }

        /// <summary>
        /// 得到search栏中相关的提示keyword
        /// </summary>
        /// <returns></returns>
        public IQueryable<keyWord> getToolTip(string keyword)
        {
            return db.Where(d => d.value.Contains(keyword));
        }

        /// <summary>
        /// 得到最热 关键字
        /// </summary>
        /// <param name="size"></param>
        /// <returns></returns>
        public IQueryable<keyWord> getByHot(int size = 20)
        {
            return db.query.OrderByDescending(d => d.num).Take(size);
        }

        /*
        /// <summary>
        /// 得到一个用户创建的所有关键字
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public IQueryable<keyWord> getByUser(string userId)
        {
            return db.Queryable.Where(d => d.userId == userId);
        }

        */

        private bool isExist(string val)
        {
            return db.Where(d => d.value == val.Trim()).Any();
        }

        private string convertVal(string val)
        {
            if (string.IsNullOrWhiteSpace(val)) throw new asMeishiException("内容不能为空！");
            return val.Trim();
        }

    }
}